package com.example.friend.mvp.ui.activity;

import android.view.ContextMenu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.friend.R;
import com.example.friend.mvp.model.ModelUser;
import com.example.friend.mvp.ui.adapter.UserAdapter;
import com.example.okhttputils.mvp.base.BaseActivity;
import com.example.okhttputils.mvp.base.BasePresenter;

import java.util.ArrayList;
import java.util.List;

/**
 * @作者: njb
 * @时间: 2019/8/6 10:36
 * @描述:
 */
public class RecyclerViewWithContenMenuActivity extends BaseActivity {
    RecyclerView rvMenu;
    private UserAdapter userAdapter;
    private List<ModelUser> modelUsers;

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.ac_context_menu ;
    }

    @Override
    protected void initView() {
        rvMenu = findViewById(R.id.rv_menu);
        registerForContextMenu(rvMenu);
        modelUsers = new ArrayList<>();
        for(int i=0;i<9;i++){
            ModelUser user = new ModelUser();
            user.setUserName("张宁");
            user.setCreatedate("2019-8-6");
            modelUsers.add(user);
        }
        userAdapter = new UserAdapter(modelUsers);
        rvMenu.setLayoutManager(new LinearLayoutManager(this));
        rvMenu.setAdapter(userAdapter);
    }

    @Override
    protected void addListener() {
        rvMenu.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() {
            @Override
            public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {

                menu.add(0,R.id.copy,0,"复制");

            }
        });
        rvMenu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                v.showContextMenu();
            }
        });
    }



     @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.delete,menu);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        int i = item.getItemId();
        if (i == R.id.copy) {
            Toast.makeText(this, "add", Toast.LENGTH_SHORT).show();
            return true;
        }
        if(i==R.id.delete){
            Toast.makeText(this, "delete", Toast.LENGTH_SHORT).show();
            return true;
        }
        return super.onContextItemSelected(item);
    }
}
